<?php

namespace App\Http\Controllers;

use App\Models\Blog;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    /**
     * 博客首页
     */
    public function index(Request $request)
    {
        // 获取关键词
        $keyword = $request->query('keyword');
        //获取分类
        $category_id = $request->query('category_id');

        $blogs = Blog::when($keyword,function ($query) use ($keyword){
            $query->where(function ($query) use ($keyword){
               $query->where('title','like',"%{$keyword}%")
                   ->orWhere('content','like',"%{$keyword}%");
            });
        })
            ->when($category_id,function ($query) use($category_id){
                $query->where('category_id',$category_id);
            })
            ->where('status',1)
            ->orderBy('updated_at','DESC')
            ->paginate(2);
        // ->dd();
        return view('index.index',['blogs'=>$blogs]);
    }
}
